#!/usr/bin/env python
#coding=utf-8

from __future__ import division
import time
import sys
from define import BASE_DIR
sys.path.append(BASE_DIR)

from lib.abstract_cron import abstract_cron
from lib.func_ext import dateTypeInterval,str2sec,sec2str,ksort

#需要修改
class gross_report_week(abstract_cron) :

    #需要修改
    #noLock = True   #是否生成脚本锁文件
    selectTable = 'sdb_statistics_business_type_report_day'
    insertTable = 'sdb_statistics_gross_report_week'

    def __init__(self) :
        #需要修改
        super(gross_report_week,self).__init__(__file__)
        self.lastWeek = dateTypeInterval('last_week')
        self.currentWeek = dateTypeInterval('week')
        curWeekNum = sec2str('%W')
        lastWeekNum = sec2str('%W',time.time()-7*24*3600)
        self.dateIdent = '(第%s周)%s/%s' % (curWeekNum,self.currentWeek[0],self.currentWeek[1])
        self.weekFormat = '(第%s周)%s/%s' % (lastWeekNum,self.lastWeek[0],self.lastWeek[1])

    def boot(self,timestamp={}) :
        #self.preBoot()
        
        bizData = self.getBizData(timestamp)
        if bizData :
            for v in bizData :
                self.figureRatio(v,'api_visit_num_total','api_fail_num_total','api_succ_ratio_total')
                v['date'] = self.weekFormat
                if not v.get('calltime') :
                    v['calltime'] = int(time.time()-1*24*3600)
                #数据过滤
                self.filterItems(v)

        #批量插入
        self.doBatchExec()

        self.sufBoot()
        print 'ok'

    def getBizData(self,timestamp={}) :
        """
            获取商铺统计项数据
        """
        timeFilter = self.getTimeFilter('calltime',timestamp)
        cols = [
            'SUM(`bind_shop`) AS bind_shop_total',
            'SUM(`api_visit_num`) AS api_visit_num_total',
            'SUM(`api_fail_num`) AS api_fail_num_total',
            'SUM(`order_num`) AS order_num_total',
            'SUM(`order_amount`) AS order_amount_total',
            'SUM(`biz_visit_num`) AS biz_visit_num_total',
            'SUM(`rel_bind_num`) AS rel_bind_num_total',
            'SUM(`rel_unbind_num`) AS rel_unbind_num_total',
            'SUM(`biz_visit_num`) AS biz_visit_num_total',
            'SUM(`bind_shop`) AS biz_num_total',
        ]

        sql  = ' SELECT '+','.join(cols)
        sql += ' FROM '+self.selectTable
        sql += ' WHERE '+self.db._filter(timeFilter)
        row = self.db.select(sql)
        if row :
            self.writeSql(sql,'succ','select')
            return row
        else :
            self.writeSql(sql,'fail','select')

    def getTimeFilter(self,timeKey,timestamp=[]) :
        keyFilter = {}
        time_begin = str2sec(self.lastWeek[0],'%Y-%m-%d')
        time_end = str2sec(self.lastWeek[1],'%Y-%m-%d')+1*24*3600
        timeIdent = timestamp.get(timeKey)
        keyFilter[timeKey+'|between'] = timeIdent and timeIdent or [time_begin,time_end]
        return keyFilter

    def insertKeys(self) :
        return [
            'api_visit_num_total','api_fail_num_total',
            'api_succ_ratio_total','order_num_total',
            'order_amount_total','rel_bind_num_total',
            'rel_unbind_num_total','biz_visit_num_total',
            'biz_num_total','date','calltime',
            ]


#需要修改
if __name__ == '__main__' :
    obj = gross_report_week()
    obj.boot()
    #timestamp = {'date':20110406}
    #obj.boot(timestamp)
